package com.example.safehome;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
import android.text.TextUtils;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button btnRegister;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 创建数据库
        SQLiteDatabase db = LitePal.getDatabase();

        // 获得控件信息
        editTextUsername = (EditText) findViewById(R.id.login_username);
        editTextPassword = (EditText) findViewById(R.id.login_password);
        btnRegister = (Button) findViewById(R.id.btn_register);
        btnLogin = (Button) findViewById(R.id.btn_login);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                // 获取EditText中的输入信息
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();

                //判断是否成功匹配的标志
                boolean flag = false;

                //LitePal里遍历查询所有数据的方法
                List<Login> users = LitePal.findAll(Login.class);
                for (Login user : users) {

                    //判断用户输入的用户名和密码是否与数据库中相同
                    if (user.getUsername().toString().equals(username) &&
                            user.getPassword().toString().equals(password)) {

                        // 判断系统用户数据库中是否已有用户记录
                        boolean inSystem = false;
                        List<System> systemUsers = LitePal.findAll(System.class);
                        for (System systemUser : systemUsers) {
                            if (systemUser.getHostName() == username) {
                                inSystem = true;
                                break;
                            }
                        }

                        // 若无则新建用户记录
                        if (inSystem == false) {
                            System system = new System();
                            system.setHostName(username);
                        }

                        // 记录当前的登录用户，以便显示信息
                        LoginUser loginUser = new LoginUser();
                        loginUser.setUsername(username);
                        flag = true;
                    }
                }

                if (flag) {

                    //创建Intent对象，传入源Activity和目的Activity的类对象
                    Intent intent = new Intent(MainActivity.this, SubSystemsChoosingActivity.class);

                    //启动Activity
                    startActivity(intent);
                    finish();
                } else {

                    //登录信息错误，通过Toast显示提示信息
                    Toast.makeText(MainActivity.this, "用户登录信息错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}